﻿using Learun.Application.Report;
using Learun.Util;
using System.Web.Mvc;
using System;

namespace Learun.Application.Web.Areas.LR_ReportModule.Controllers
{
    /// <summary>
    /// 版 本 PIT-ADMS V7.0.3 敏捷开发框架
    /// Copyright (c) 2013-2018 Hexagon PPM
    /// 创 建：超级管理员
    /// 日 期：2017-07-12 09:57
    /// 描 述：报表管理
    /// </summary>
    public class ReportManageController : MvcControllerBase
    {
        private ReportTempIBLL reportTempIBLL = new ReportTempBLL();

        #region 视图功能
        /// <summary>
        /// 管理页面
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 表单页面
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Form()
        {
            return View();
        }
        /// <summary>
        /// 浏览页面
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Preview()
        {
            return View();
        }
        #endregion

        #region 获取数据
        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <param name="pagination">分页参数</param>
        /// <param name="keyword">关键词</param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetPageList(string pagination, string keyword)
        {
            Pagination paginationobj = pagination.ToObject<Pagination>();
            var data = reportTempIBLL.GetPageList(paginationobj, keyword);
            var jsonData = new
            {
                rows = data,
                total = paginationobj.total,
                page = paginationobj.page,
                records = paginationobj.records,
            };
            return Success(jsonData);
        }
        /// <summary>
        /// 获取实体数据
        /// </summary>
        /// <param name="keyValue">主键</param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetEntity(string keyValue)
        {
            var data = reportTempIBLL.GetEntity(keyValue);
            return Success(data);
        }
        /// <summary>
        /// 获取报表数据
        /// </summary>
        /// <param name="reportId">报表主键</param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetReportData(string reportId)
        {
            ReportTempEntity reportEntity = reportTempIBLL.GetEntity(reportId);
            dynamic paramJson = reportEntity.F_ParamJson.ToJson();
            var data = new
            {
                tempStyle = reportEntity.F_TempStyle,
                chartType = reportEntity.F_TempType,
                chartData = reportTempIBLL.GetReportData(paramJson.F_DataSourceId.ToString(), paramJson.F_ChartSqlString.ToString()),
                listData = reportTempIBLL.GetReportData(paramJson.F_DataSourceId.ToString(), paramJson.F_ListSqlString.ToString())
            };
            return Content(data.ToJson());
        }
        #endregion

        #region 提交数据
        /// <summary>
        /// 保存表单数据
        /// </summary>
        /// <param name="keyValue">主键</param>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        [HttpPost, ValidateAntiForgeryToken, AjaxOnly]
        public ActionResult SaveForm(string keyValue, ReportTempEntity entity)
        {
            reportTempIBLL.SaveEntity(keyValue, entity);
            return Success("保存成功！");
        }
        /// <summary>
        /// 删除表单数据
        /// </summary>
        /// <param name="keyValue">主键</param>
        /// <returns></returns>
        [HttpPost]
        [AjaxOnly]
        public ActionResult DeleteForm(string keyValue)
        {
            reportTempIBLL.DeleteEntity(keyValue);
            return Success("删除成功！");
        }
        #endregion

        #region 看板查询获取数据
        /// <summary>
        /// 看板查询获取数据
        /// </summary>
        /// <param name="InputData">入参，多参数以~分开，例如入参2个就是2019-01-01~2019-02-02，入参一个2019-01-01~，入参为空则~</param>
        /// <param name="Type">类型</param>
        /// <param name="SourceId">数据源Id，默认systemdb本地数据库</param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetKanBanData(string InputData, string Type,string SourceId= "systemdb")
        {
            var keys = new string[0];
            if (!InputData.IsEmpty())
                keys = InputData.Split('~');
            string sql = "";
            #region 模具及时借出率
            if (Type == "MmsLendData")
            {
                 sql = "select a.*,b.实际借出数量 from ( "
                    + "select year(PlanLendDate) as 年,month(PlanLendDate) as 月,count(*) as 计划借出数量 "
                    + "from MES_Mms_MouldUsingStatus where PlanLendDate is not null and Enable = 1 "
                    + "group by  year(PlanLendDate),month(PlanLendDate) ) as a "
                    + "left join( "
                    + "select year(ActiveLendDate) as 年,month(ActiveLendDate) as 月,count(*) as 实际借出数量 "
                    + "from MES_Mms_MouldUsingStatus where Enable = 1 and ActiveLendDate is not null "
                    + "group by  year(ActiveLendDate),month(ActiveLendDate) ) as b on a.年 = b.年 and a.月 = b.月 and a.年=" + Convert.ToInt32(keys[0]);                             
            }
            #endregion
            #region 模具及时归还率
            else if (Type == "MmsReturnData")
            {
                 sql = "select a.*,b.实际归还数量 from ( "
                    + "select year(PlanReturnDate) as 年,month(PlanReturnDate) as 月,count(*) as 计划归还数量 "
                    + "from MES_Mms_MouldUsingStatus where PlanReturnDate is not null and Enable=1 "
                    + "group by  year(PlanReturnDate),month(PlanReturnDate) ) as a "
                    + "left join ( "
                    + "select year(ActiveReturnDate) as 年,month(ActiveReturnDate) as 月,count(*) as 实际归还数量 "
                    + "from MES_Mms_MouldUsingStatus where ActiveReturnDate is not null and Enable=1 "
                    + "group by  year(ActiveReturnDate),month(ActiveReturnDate) ) as b on a.年=b.年 and a.月= b.月 and a.年=" + Convert.ToInt32(keys[0]);
            }
            #endregion
            #region 今日待借出
            else if (Type == "MmsTodayToBeLend")
            {
                sql = "select a.AccountCode as 模具编码,b.Description as 模具描述,a.PlanLendDate as 计划领用时间, "
                    + "a.availabledate as 可领用时间,a.Users as 借出人,a.Groups as 占用产线,a.UsingStatus as 状态 "
                    + "from MES_Mms_MouldUsingStatus a "
                    + "left join MES_Mms_MouldAccount b on a.AccountCode=b.AccountCode "
                    + "where datediff(dd,a.PlanLendDate,GETDATE())=0 and a.UsingStatus='ToBeLent' and a.Enable=1 and a.groups='" + keys[0] + "'";
            }
            #endregion
            #region 今日待归还
            else if (Type == "MmsTodayToBeLend")
            {
                sql = "select a.AccountCode as 模具编码,b.Description as 模具描述,a.PlanLendDate as 计划领用时间, "
                    + "a.availabledate as 可领用时间,a.Users as 归还人,a.Groups as 占用产线,a.UsingStatus as 状态 "
                    + "from MES_Mms_MouldUsingStatus a "
                    + "left join MES_Mms_MouldAccount b on a.AccountCode=b.AccountCode "
                    + "where datediff(dd,a.PlanLendDate,GETDATE())=0 and a.UsingStatus='ToBeReturn' and a.Enable=1 and a.groups='" + keys[0] + "'";
            }
            #endregion
            #region 异常看板，异常关闭数
            else if (Type == "AmsTotal")
            {
                sql = "select 天数='000-001',count(*) as '数量' from Ams_AnomalAccount "
                   + "where ProcessStatus!='Processed' and DateDiff(dd,ReportingTime,getdate())<=1 and DateDiff(dd,ReportingTime,getdate())>0 "
                   + "union select 天数 = '001-003', count(*) as '数量' from Ams_AnomalAccount "
                   + "where ProcessStatus!='Processed' and DateDiff(dd,ReportingTime,getdate())<=3 and DateDiff(dd,ReportingTime,getdate())>1 "
                   + "union select 天数 = '003-007', count(*) as '数量' from Ams_AnomalAccount "
                   + "where ProcessStatus!='Processed' and DateDiff(dd,ReportingTime,getdate())<=7 and DateDiff(dd,ReportingTime,getdate())>3 "
                   + "union select 天数 = '007-014', count(*) as '数量' from Ams_AnomalAccount  "
                   + "where ProcessStatus!='Processed' and DateDiff(dd,ReportingTime,getdate())<=14 and DateDiff(dd,ReportingTime,getdate())>7 "
                   + "union select 天数 = '014-030', count(*) as '数量' from Ams_AnomalAccount "
                   + "where ProcessStatus!='Processed' and DateDiff(dd,ReportingTime,getdate())<=30 and DateDiff(dd,ReportingTime,getdate())>14 "
                   + "union select 天数 = '030-090', count(*) as '数量' from Ams_AnomalAccount "
                   + "where ProcessStatus!='Processed' and DateDiff(dd,ReportingTime,getdate())<=90 and DateDiff(dd,ReportingTime,getdate())>30 "
                   + "union select 天数 = '090-180', count(*) as '数量' from Ams_AnomalAccount "
                   + "where ProcessStatus!='Processed' and DateDiff(dd,ReportingTime,getdate())<=180 and DateDiff(dd,ReportingTime,getdate())>90 "
                   + "union select 天数 = '180-~', count(*) as '数量' from Ams_AnomalAccount "
                   + "where ProcessStatus!='Processed' and DateDiff(dd,ReportingTime,getdate())>180";
            }
            #endregion
            #region 异常看板，超过七天未关闭数量
            else if (Type == "AmsTypeTotal")
            {
                sql = "select a.ResponsibleDept,count(a.ResponsibleDept) as '数量' from Ams_AnomalAccount a "
                   + "where a.ProcessStatus!='Processed' and DateDiff(dd,a.ReportingTime,getdate())>=7 group by a.ResponsibleDept";
            }
            #endregion
            #region 异常看板，异常提报分类
            else if (Type == "AmsTypeTotal")
            {
                sql = "select b.TypeName,count(b.TypeName) as '数量' from Ams_AnomalAccount a "
                   + "left join Ams_AnomalyType b on a.AbnormalType=b.Id "
                   + "where a.ProcessStatus!='Processed' and a.ReportingTime >="+keys[0]+ "and a.ReportingTime <=" + keys[1]+ "and a.ResponsibleDept ='" + keys[2]+"'"
                   + " group by b.TypeName";
            }
            #endregion
            #region 异常未关闭数
            #endregion
            var data = reportTempIBLL.GetReportData(SourceId, sql);
            return Success(data.ToJson());
        }
        #endregion
    }
}